package com.example.templateapp.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.templateapp.R


class ActionRecyclerView : FrameLayout {

    constructor(context: Context) : this(context, null) {
        Log.d("customView", "ActionRecyclerView.this(context, null)")
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        Log.d("customView", "ActionRecyclerView.this(context, attrs, 0),attrs=$attrs")
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
            : super(context, attrs, defStyleAttr, 0) {
        Log.d("customView", "ActionRecyclerView.super(context, attrs, 0),attrs=$attrs")
        initView(context, attrs)
    }

    private lateinit var progressLayout: LinearLayout
    private lateinit var tipLayout: LinearLayout
    private lateinit var rv: RecyclerView
    private lateinit var progressTv: TextView
    private lateinit var tipTv: TextView

    private lateinit var tipIv: ImageView

    private fun initView(context: Context, attrs: AttributeSet?) {
        Log.d("customView", "ActionRecyclerView.initView")
        LayoutInflater.from(context).inflate(R.layout.layout_action_recycler_view, this)
        progressLayout = findViewById(R.id.progress_layout)
        tipLayout = findViewById(R.id.tip_layout)
        rv = findViewById(R.id.rv)
        progressTv = findViewById(R.id.progress_tv)
        tipTv = findViewById(R.id.tip_tv)
        tipIv = findViewById(R.id.tip_iv)
    }

    fun getRecyclerView(): RecyclerView {
        return rv
    }

    fun tip(tipMsg: String = "暂无数据", tipImgResId: Int = R.drawable.ic_announcement) {
        tipLayout.visibility = View.VISIBLE
        progressLayout.visibility = View.GONE
        rv.visibility = View.GONE

        tipTv.text = tipMsg
        tipIv.setImageDrawable(context.getDrawable(tipImgResId))
    }

    fun loading(loadingMsg: String = "加载中") {
        tipLayout.visibility = View.GONE
        progressLayout.visibility = View.VISIBLE
        rv.visibility = View.GONE
    }

    fun disLoading() {
        progressLayout.visibility = View.GONE
    }

    fun showRecyclerView() {
        rv.visibility = View.VISIBLE
        progressLayout.visibility = View.GONE
        tipLayout.visibility = View.GONE
    }

}